/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.com.createpdf;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import model.Product;
import model.Staff;

/**
 *
 * @author Trang
 */
public class CreateInventoryReport {
    //Date createdate = new Date();
    //String date;
    //SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    //String date = dateFormat.format(createdate);
    public static final String RESULT
        = "E:\\receipt3.pdf";
    public static PdfPTable createTableHeader(String date) {
    	// a table with three columns
        PdfPTable table = new PdfPTable(2);
        
        // the cell object
        PdfPCell cell;
        
        // cell with 
        Font f1 = new Font();
        f1.setSize(11);
        //f1.isBold();
        cell = new PdfPCell(new Phrase("COMPUTER PRODUCT RETAILER", f1));
        cell.setBorderColor(Color.WHITE);
        cell.setColspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        //cell.setColspan(3);
        
        table.addCell(cell);
        // now we add a cell with rowspan 2
        cell = new PdfPCell(new Phrase("23 Nguyen Trai - Thanh Xuan - Ha Noi", f1));
        cell.setBorderColor(Color.WHITE);
        cell.setColspan(2);
        table.addCell(cell);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        // we add the four remaining cells with addCell()
        cell = new PdfPCell(new Phrase("0982396012/0914555444", f1));
        cell.setBorderColor(Color.WHITE);
        cell.setColspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        //cell.setColspan(3);
        
        table.addCell(cell);
        
        Font f = new Font();
        f.setSize(15);
        f.isBold();
        Phrase paragrah = new Phrase("Inventory Note",f);

        cell = new PdfPCell(paragrah);
        cell.setBorderColor(Color.WHITE);
        cell.setColspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        
        //Date date = new Date();
        cell = new PdfPCell(new Phrase(date,f1));
        cell.setBorderColor(Color.WHITE);
        cell.setColspan(2);
        
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        return table;
    }
     public static PdfPTable createSecondTable( List<Product> receiptitem, Staff staff) {
    	// a table with three columns
         
        PdfPTable table = new PdfPTable(7);
        table.getDefaultCell().setBorderColor(Color.BLACK);
        //table.getDefaultCell().setColumn(null);
        Font f = new Font();
        f.setSize(11);
        f.isBold();
        f.isItalic();
       
        table.addCell(new PdfPCell(new Phrase("No",f)));
        table.addCell(new PdfPCell(new Phrase("Product Name",f)));
        table.addCell(new PdfPCell(new Phrase("Product Code",f)));
        table.addCell(new PdfPCell(new Phrase("Product Brand",f)));
        table.addCell(new PdfPCell(new Phrase("Category",f)));
        table.addCell(new PdfPCell(new Phrase("Unit Price ($)",f)));
        table.addCell(new PdfPCell(new Phrase("Stock Quantity",f)));
        int i = 1;
        double totalAmount = 0;
        for(Product item: receiptitem)
        {
            //totalAmount += item.getReceiptQuantity()*item.getReceiptPrice();
            table.addCell(String.format("%d",i));
            table.addCell(item.getName());
            table.addCell(item.getCode());
            table.addCell(item.getBrand());
            table.addCell(item.getCategory().getName());
            table.addCell(String.format("%.2f %n", item.getUnitPrice()));
            table.addCell(String.format("%d", item.getStockQuantity()));
            
            //table.addCell(String.format("%.2f %n",item.getReceiptQuantity()*item.getReceiptPrice()));
            i++;
        }
        //table.addCell(new PdfPCell(new Phrase("Total amount",f)));
        PdfPCell cell2 = new PdfPCell(new Phrase( "Report Creater",f));
        cell2.setBorderColor(Color.WHITE);
        cell2.setColspan(7);
        //table.addCell(cell);
        cell2.setHorizontalAlignment(Element.ALIGN_RIGHT);
        
//        PdfPCell cell = new PdfPCell(new Phrase(String.format("%.2f %n", totalAmount)));
//        
//        cell.setColspan(5);
        
        //cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(cell2);
        PdfPCell cell = new PdfPCell(new Phrase( staff.getLastname()+" "+staff.getFirstname(),f));
        cell.setBorderColor(Color.WHITE);
        cell.setColspan(7);
        //table.addCell(cell);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        
//        PdfPCell cell = new PdfPCell(new Phrase(String.format("%.2f %n", totalAmount)));
//        
//        cell.setColspan(5);
        
        //cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(cell);
        return table;
    }
      public void createPdf(String date, String filename, List<Product> products, Staff staff)
        throws IOException, DocumentException {
//        Receipt receip = receiptingDAO.getReceipFromReceipID(14);
//        List<Receiptitem> items = receiptingDAO.getReceiptitemFromReceipID(14);
    	// step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        
        //PdfWriter.getInstance(document, null)
        // step 3
        document.open();
        // step 4
        document.add(createTableHeader(date));
         
        
        document.add(createSecondTable(products, staff));
        // step 5
        document.close();
    }
}
